package com.nibian.nibian.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nibian.nibian.R;
import com.nibian.nibian.activity.MainActivity;
import com.nibian.nibian.activity.MessageActivity;
import com.nibian.nibian.bean.TextBean;
import com.nibian.nibian.ui.ProgressDialogs;
import com.nibian.nibian.utils.DateUtil;
import com.nibian.nibian.utils.SharedPreferenceTools;
import com.nibian.nibian.utils.UiUtils;
import com.nibianplus.base.BaseFragment;
import com.nibianplus.base.BaseObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @创建者 luck
 * @创建时间 ${DATA} 15:00
 * @描述 ${历史数据}
 * @更新者 $Author$
 * @更新时间 $Date$
 * @更新描述 ${TODO}
 */
public class ShuJuFragment extends BaseFragment {

    @BindView(R.id.linear_one)
    LinearLayout linear_one;            //点击详情
    @BindView(R.id.linear_two)
    LinearLayout linear_two;
    @BindView(R.id.linear_three)
    LinearLayout linear_three;
    @BindView(R.id.linear_four)
    LinearLayout linear_four;
    @BindView(R.id.tv_zddy)
    TextView tv_zddy;                   //蓄电池当天最低电压
    @BindView(R.id.tv_zgdy)
    TextView tv_zgdy;                   //蓄电池最高电压
    @BindView(R.id.tv_yxts)
    TextView tv_yxts;                   //运行天数
    @BindView(R.id.tv_gfcs)
    TextView tv_gfcs;                   //过放次数
    @BindView(R.id.tv_cmcs)
    TextView tv_cmcs;                   //充满次数
    @BindView(R.id.tv_zcda)
    TextView tv_zcda;                   //总充电安
    //    @BindView(R.id.tv_zfda)
//    TextView tv_zfda;                   //总放电安
    @BindView(R.id.tv_ljfdl)
    TextView tv_ljfdl;                  //累计发电量
    //    @BindView(R.id.tv_ljyjl)
//    TextView tv_ljyjl;                  //累计用计量
    @BindView(R.id.tv_fdl)
    TextView tv_fdl;                    //发电量
    //    @BindView(R.id.tv_hdl)
//    TextView tv_hdl;                    //耗电量
    @BindView(R.id.tv_cdzd)
    TextView tv_cdzd;                   //充电最大功率
    //    @BindView(R.id.tv_fdzd)
//    TextView tv_fdzd;                   //放带你最大功率
    @BindView(R.id.tv_cda)
    TextView tv_cda;                    //充电安
    //    @BindView(R.id.tv_fda)
//    TextView tv_fda;                    //放电安
    @BindView(R.id.tv_showtime)
    TextView tv_showtime;               //时间展示
    @BindView(R.id.ll_uptoday)
    LinearLayout ll_uptoday;            //前一天
    @BindView(R.id.rl_downtoday)
    RelativeLayout rl_downtoday;        //后一天
    private String mSpecifiedDay;       //当天事时间
    private String mToday;              //今天时间
    private Dialog loading;             //加载框
    public static ShuJuFragment sShuJuFragment;
    private String mLanguages_key;
    private View mInflate;


    public static ShuJuFragment getInstance() {
        return sShuJuFragment;
    }

    /**
     * init - view
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        sShuJuFragment = this;
        mLanguages_key = SharedPreferenceTools.getString(UiUtils.getContext(), "LANGUAGES_KEY", "zh");
        if (mLanguages_key.equals("zh")) {
            loading = ProgressDialogs.getLoading(getActivity(), "正在加载...");
        } else {
            loading = ProgressDialogs.getLoading(getActivity(), "loading...");
        }
        loading.show();
        if (mInflate == null) {
            mInflate = View.inflate(UiUtils.getContext(), R.layout.history_data, null);
            ButterKnife.bind(this, mInflate);
            getTodayTime();
            MainActivity.getInstance().sendShuJuData();
            MainActivity.getInstance().showJianKongFra();
        }
        return mInflate;
    }

    /**
     * 获取当前时间并设置数据
     */
    private void getTodayTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        mSpecifiedDay = sdf.format(date);
        mToday = mSpecifiedDay;
        tv_showtime.setText(mSpecifiedDay);
    }

    /**
     * 事件处理
     *
     * @param v
     */
    @OnClick({R.id.linear_one, R.id.linear_two, R.id.linear_three, R.id.linear_four, R.id.ll_uptoday, R.id.rl_downtoday})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linear_one:                   //详情界面
                Intent intent = new Intent(UiUtils.getContext(), MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.linear_two:                   //详情界面
                Intent intent1 = new Intent(UiUtils.getContext(), MessageActivity.class);
                startActivity(intent1);
                break;
            case R.id.linear_three:                 //详情界面
                Intent intent2 = new Intent(UiUtils.getContext(), MessageActivity.class);
                startActivity(intent2);
                break;
            case R.id.linear_four:                  //详情界面
                Intent intent3 = new Intent(UiUtils.getContext(), MessageActivity.class);
                startActivity(intent3);
                break;
            case R.id.ll_uptoday:                   //前一天
                String specifiedDayBefore = DateUtil.getSpecifiedDayBefore(mSpecifiedDay);
                tv_showtime.setText(specifiedDayBefore);
                mSpecifiedDay = specifiedDayBefore;
                break;
            case R.id.rl_downtoday:                  //后一天
                String specifiedDayAfter = DateUtil.getSpecifiedDayAfter(mSpecifiedDay);
                if (mToday.equals(tv_showtime.getText())) {
                    UiUtils.toast("暂无明天数据");
                    return;
                }
                tv_showtime.setText(specifiedDayAfter);
                mSpecifiedDay = specifiedDayAfter;
                break;
        }
    }

    /**
     * diolog 取消
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (loading != null) {
            loading.cancel();
        }
    }

    @Override
    public void refreshUI(BaseObject object, int what) {
    }

    @Override
    protected BaseObject createDataObject() {
        return TextBean.getInstance(getTagName());
    }


    public void setUiData2(String data, int dataCount) {
        if (dataCount == 0) {
            String substring = data.substring(14, 18);
            String zddy = String.format("%.1f", UiUtils.toTen(substring) * 0.1);
            tv_zddy.setText(zddy + "V");

            String substring2 = data.substring(18, 22);                       //第六、七位0084H表示当天的蓄电池最高电压
            String zgdy = String.format("%.1f", UiUtils.toTen(substring2) * 0.1);
            tv_zgdy.setText(zgdy + "V");

            String substring_4 = data.substring(22, 26);
            String zfda = String.format("%.2f", UiUtils.toTen(substring_4) * 0.01);              //蓄电池总放电安时数
            tv_gfcs.setText(zfda + "A");

        } else if (dataCount == 1) {
            //ffff 020b 01 03 06 0009 0000 0000 fd74 aaaa
            String substring3 = data.substring(14, 18);                       //第四、五字节为运行天数
            int yxts = UiUtils.toTen(substring3);
            tv_yxts.setText(yxts + "天");
//            String substring4 = data.substring(18, 22);                       //第六、七字节0001H为过放次数
//            int gfcs = UiUtils.toTen(substring4);
//            tv_gfcs.setText(gfcs + "次");
            String substring5 = data.substring(22, 26);                       //第八、九字节0006H为充满次数
            int cmcs = UiUtils.toTen(substring5);
            tv_cmcs.setText(cmcs + "次");
        } else if (dataCount == 2) {
            //读蓄电池总充电安时数和蓄电池总放电安时数
            //ffff 020d 01 03 08 0000 0000 0000 0009 5d7aaaa
            String substring_3 = data.substring(14, 22);
            String zcda = String.format("%.3f", UiUtils.toTen(substring_3) * 0.001);              //为蓄电池总充电安时数
            tv_zcda.setText(zcda + "KAH");

        } else if (dataCount == 3) {
            //读控制器累计发电量和累计放电量
            ////ffff 020d 01 03 08 0000 0000 0000 0009 5d7aaaa
            String sub4 = data.substring(14, 22);                                                     //累计发电量
            float ljfdl = UiUtils.toTen(sub4) * 1.0f / 1000;
            tv_ljfdl.setText(ljfdl + "°");
            String sub5 = data.substring(22, 30);                                                     //累计用电量
//                int ljydl = UiUtils.toTen(sub5);
//                tv_ljyjl.setText(ljydl + "°");

        } else if (dataCount == 4) {
            //当天发电量 - 当天耗电量5 -
            String subb5 = data.substring(14, 18);
            int fdl = UiUtils.toTen(subb5);
            tv_fdl.setText((fdl * 1.0f / 1000) + "℃");
            String subbb5 = data.substring(18, 22);
//                int hdl = UiUtils.toTen(subbb5);
//                tv_hdl.setText(hdl/1000 +"℃");

        } else if (dataCount == 5) {
            //当天充电最大功率 - 放电最大功率6  ffff 0209 0103 04 0000 0000 fa33 aaaa
            String sub6 = data.substring(14, 18);
            int cdzdgn = UiUtils.toTen(sub6);
            tv_cdzd.setText(cdzdgn + "W");
            String sub66 = data.substring(18, 22);
//                int fdzdgn = UiUtils.toTen(sub66);
//                tv_fdzd.setText(fdzdgn +"W");
        } else if (dataCount == 6) {
            //当天充电安时数7 - 第四、五字节0608H为当天充电安时数，即十进制1544AH;
            String sub7 = data.substring(14, 18);
            int cda = UiUtils.toTen(sub7);
            tv_cda.setText(cda + "AH");
            String sub77 = data.substring(18, 22);
        }


    }

    /**
     * 设置Ui数据
     *
     * @param list
     */
    public void setUiData(List<String> list) {
        if (list != null && list.size() >= 8) {                                                 //ffff020b 01 03 06 007a 007b 0000 88a6 aaaa
            try {
                // [ffff000c5452414e53504f52543d320daaaa, ffff020b010306 007a007a0000d966aaaa, ffff020b010306000c000000003174aaaa, ffff020d010308000000000000000095d7aaaa, ffff020d010308000000000000000095d7aaaa]
                Log.d("mListData2", list + "");
                String s = list.get(1);                                        //四、五位0070H表示当天的蓄电池最低电压
                String substring = s.substring(14, 18);
                String zddy = String.format("%.1f", UiUtils.toTen(substring) * 0.1);
                tv_zddy.setText(zddy + "V");

                String substring2 = s.substring(18, 22);                       //第六、七位0084H表示当天的蓄电池最高电压
                String zgdy = String.format("%.1f", UiUtils.toTen(substring2) * 0.1);
                tv_zgdy.setText(zgdy + "V");

                //ffff 020b 01 03 06 0009 0000 0000 fd74 aaaa
                String s1 = list.get(2);
                String substring3 = s1.substring(14, 18);                       //第四、五字节为运行天数
                int yxts = UiUtils.toTen(substring3);
                tv_yxts.setText(yxts + "天");
                String substring4 = s1.substring(18, 22);                       //第六、七字节0001H为过放次数
                int gfcs = UiUtils.toTen(substring4);
                tv_gfcs.setText(gfcs + "次");
                String substring5 = s1.substring(22, 26);                       //第八、九字节0006H为充满次数
                int cmcs = UiUtils.toTen(substring5);
                tv_cmcs.setText(cmcs + "次");

                //读蓄电池总充电安时数和蓄电池总放电安时数
                //ffff 020d 01 03 08 0000 0000 0000 0009 5d7aaaa
                String s3 = list.get(3);
                String substring_3 = s3.substring(14, 22);
                String zcda = String.format("%.3f", UiUtils.toTen(substring_3) * 0.001);              //为蓄电池总充电安时数
                tv_zcda.setText(zcda + "KAH");
                String substring_4 = s3.substring(22, 30);
//                String zfda = String.format("%.3f", UiUtils.toTen(substring_4) * 0.001);              //蓄电池总放电安时数
//                tv_zfda.setText(zfda + "KAH");

                //读控制器累计发电量和累计放电量
                ////ffff 020d 01 03 08 0000 0000 0000 0009 5d7aaaa
                String s4 = list.get(4);
                String sub4 = s4.substring(14, 22);                                                     //累计发电量
                int ljfdl = UiUtils.toTen(sub4);
                tv_ljfdl.setText(ljfdl + "°");
                String sub5 = s4.substring(22, 30);                                                     //累计用电量
//                int ljydl = UiUtils.toTen(sub5);
//                tv_ljyjl.setText(ljydl + "°");

                //当天发电量 - 当天耗电量5 -
                String s5 = list.get(5);
                String subb5 = s5.substring(14, 18);
                int fdl = UiUtils.toTen(subb5);
                tv_fdl.setText(fdl / 1000 + "℃");
                String subbb5 = s5.substring(18, 22);
//                int hdl = UiUtils.toTen(subbb5);
//                tv_hdl.setText(hdl/1000 +"℃");

                //当天充电最大功率 - 放电最大功率6  ffff 0209 0103 04 0000 0000 fa33 aaaa
                String s6 = list.get(6);
                String sub6 = s6.substring(14, 18);
                int cdzdgn = UiUtils.toTen(sub6);
                tv_cdzd.setText(cdzdgn + "W");
                String sub66 = s6.substring(18, 22);
//                int fdzdgn = UiUtils.toTen(sub66);
//                tv_fdzd.setText(fdzdgn +"W");

                //当天充电安时数7 - 第四、五字节0608H为当天充电安时数，即十进制1544AH;
                String s7 = list.get(7);
                String sub7 = s7.substring(14, 18);
                int cda = UiUtils.toTen(sub7);
                tv_cda.setText(cda + "AH");
                String sub77 = s7.substring(18, 22);
//                int fda = UiUtils.toTen(sub77);
//                tv_fda.setText(fda +"AH");
            } catch (Exception e) {

            }
            list.clear();
        }
    }
}
